﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Projeto3D
{
    class TiroManager
    {
        public List<Tiro> tiroList;
        public float delayTiroMillisec;

        private List<Tiro> tiroToRemoveList;

        public TiroManager()
        {
            this.tiroList = new List<Tiro>();
            this.tiroToRemoveList = new List<Tiro>();

            this.delayTiroMillisec = 100f;
        }

        public void draw(Camera3D camera)
        {
            foreach (Tiro tiro in tiroList)
            {
                tiro.draw(camera);
            }
        }

        public void update()
        {
            foreach (Tiro tiro in tiroList)
            {
                tiro.update();

                if (Math.Abs(tiro.position.X) >= 200 || Math.Abs(tiro.position.Z) >= 200)
                {
                    tiroToRemoveList.Add(tiro);
                }
            }

            foreach (Tiro tiro in tiroToRemoveList)
            {
                tiroList.Remove(tiro);
            }

            //ColideTiro();
        }

        //static public void ColideTiro()
        //{
        //    for (int i = 0; i < tiroList.Count; i++ )
        //    {
        //        Tiro tiro = tiroList[i];
        //        foreach (Inimigo inimigo in InimigoManager.listaInimigo)
        //        {
        //            if (tiro.hitTestObject(inimigo))
        //            {
        //                tiroList.Remove(tiro);
        //                //FIX FIX Fazer dano
        //                Pontuacao.Ganharpontos(10);
        //                InimigoManager.listaInimigo.Remove(inimigo);
        //                break;
        //            }
        //        }
        //    }
        //}

        private Vector3 MousePosition(MouseState ms, Camera3D camera)
        {
            Vector3 nearScreenPoint = new Vector3(ms.X, ms.Y, 0);
            Vector3 farScreenPoint = new Vector3(ms.X, ms.Y, 1);
            Vector3 nearWorldPoint = Game1.self.GraphicsDevice.Viewport.Unproject(nearScreenPoint, camera.projection, camera.View, Matrix.Identity);
            Vector3 farWorldPoint = Game1.self.GraphicsDevice.Viewport.Unproject(farScreenPoint, camera.projection, camera.View, Matrix.Identity);

            Vector3 direction = farWorldPoint - nearWorldPoint;

            float zFactor = -nearWorldPoint.Y / direction.Y;
            Vector3 zeroWorldPoint = nearWorldPoint + direction * zFactor;

            return zeroWorldPoint;
        }

        public void createTiro(Tiro.Tipo tipo, Vector3 posicao, MouseState mouseState, Camera3D camera)
        {
            Vector3 temp = MousePosition(mouseState, camera);

            tiroList.Add(getTiro(tipo, posicao, temp - posicao));
        }

        public Tiro getTiro(Tiro.Tipo tipo, Vector3 posicao, Vector3 direcao)
        {
            Tiro tiro;

            switch (tipo)
            {
                case Tiro.Tipo.Multishoot:
                    tiro = new Tiro(Game1.self.Content.Load<Model>("tiro"), posicao, direcao, tipo);
                    break;
                case Tiro.Tipo.Laser:
                    tiro = new Tiro(Game1.self.Content.Load<Model>("tiro"), posicao, direcao, tipo);
                    break;
                case Tiro.Tipo.LancaChamas:
                    tiro = new Tiro(Game1.self.Content.Load<Model>("tiro"), posicao, direcao, tipo);
                    break;
                default:
                    tiro = new Tiro(Game1.self.Content.Load<Model>("tiro"), posicao, direcao, tipo);
                    break;
            }

            return tiro;
        }
    }
}
